package com.example.helloword.dao;
import com.example.helloword.pojo.Department;
import com.example.helloword.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @create 2022-03-2022/3/9-20:47
 */
@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employees = null;

    @Autowired
    private DepartmentDao departmentDao;

    static {
        employees = new HashMap<Integer, Employee>();
        employees.put(101,new Employee(101,"Tom","1111111",1,new Department(1,"生产部"),new Date()));
        employees.put(102,new Employee(102,"Jack","2222222",0,new Department(2,"运输部"),new Date()));
        employees.put(103,new Employee(103,"Lisa","3333333",0,new Department(3,"销售部"),new Date()));
        employees.put(104,new Employee(104,"Kerry","4444444",1,new Department(4,"人事部"),new Date()));
        employees.put(105,new Employee(105,"Lao8","5555555",0,new Department(5,"质检部"),new Date()));
    }

    private static Integer initId =106;
    public void  saveEmployee(Employee employee){
        if(employee.getId()==null){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDeparmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);
    }


    public Collection<Employee> getAllEmployee(){
        return  employees.values();
    }

    public  Employee getEmployeeById(Integer id){
        return employees.get(id);
    }

    public void deleteById(Integer id){
        employees.remove(id);
    }


}
